package com.feng.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.feng.domain.ResponseResult;
import com.feng.domain.dto.AddArticleDto;
import com.feng.domain.dto.ArticleDto;
import com.feng.domain.dto.CategorySaveDto;
import com.feng.domain.entity.Category;
import com.feng.domain.entity.Menu;
import com.feng.domain.enums.AppHttpCodeEnum;
import com.feng.domain.vo.ArticleUpdateVo;
import com.feng.domain.vo.CategoryPageVo;
import com.feng.domain.vo.CategoryVo;
import com.feng.domain.vo.ExcelCategoryVo;
import com.feng.service.ArticleService;
import com.feng.service.CategoryService;
import com.feng.service.MenuService;
import com.feng.service.TagService;
import com.feng.util.BeanCopyUtils;
import com.feng.util.WebUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/content")
public class ContentController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private TagService tagService;
    @Autowired
    private MenuService menuService;

    @GetMapping("/category/listAllCategory")
    public ResponseResult<CategoryVo> listAllCategoory() {
        return categoryService.listAllCategoory();
    }

    @PostMapping("/article")
    public ResponseResult add(@RequestBody AddArticleDto article) {
        return articleService.add(article);
    }

    @GetMapping("/category/list")
    public ResponseResult pagecategorylist(Integer pageNum, Integer pageSize, String name, String status) {
        return categoryService.pagecategorylist(pageNum, pageSize, name, status);
    }

    @PostMapping("/category")
    public ResponseResult addCategory(@RequestBody CategorySaveDto categorySaveDto) {
        return categoryService.addCategory(categorySaveDto);
    }

    @GetMapping("/category/{id}")
    public ResponseResult getCategoryById(@PathVariable Long id) {
        return categoryService.getCategoryById(id);
    }

    @PutMapping("/category")
    public ResponseResult updateCategoryById(@RequestBody CategoryPageVo categoryPageVo) {
        return categoryService.updateCategoryById(categoryPageVo);
    }

    @DeleteMapping("/category/{id}")
    public ResponseResult deleteCategoryById(@PathVariable List<Long> id) {
        return categoryService.deleteCategoryById(id);
    }

    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/category/export")
    public void export(HttpServletResponse response) {
        //设置下载文件的请求头
        try {
            WebUtils.setDownLoadHeader("分类.xlsx", response);

            //获取需要导出的数据
            List<Category> list = categoryService.list();
            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(list, ExcelCategoryVo.class);
            //把数据写入到excel中

            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVos);
        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
            //如果出现异常也要响应Json数据
        }

    }

    @GetMapping("/article/list")
    public ResponseResult pageListArticle(ArticleDto articleDto) {
        return articleService.pageListArticle(articleDto);

    }
    @GetMapping("/article/{id}")
    public ResponseResult findArticleById(@PathVariable Long id){
        return articleService.findArticleById(id);
    }
    @PutMapping("/article")
    public ResponseResult updateArticleById(@RequestBody ArticleUpdateVo articleUpdateVo){
        return articleService.updateArticleById(articleUpdateVo);
    }
    @DeleteMapping("/article/{id}")
    public ResponseResult deleArticleById(@PathVariable List<Long> id){
        return articleService.deleArticleById(id);
    }


}
